package core;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Regex {
	
	private Pattern p;
	private Matcher m;
	private HashMap<Integer,String> mp;
	
	Regex() {
		mp = new HashMap<Integer,String>();
	}

	public boolean check(String regex, String text) {
		p = Pattern.compile(regex); // the expression 
    	m = p.matcher(text); // the source
    	return m.find();
	}
	
	public HashMap<Integer,String> getResult(String regex, String text) {
		//s.delete(0, s.length());
		mp.clear();
		p = Pattern.compile(regex); // the expression 
    	m = p.matcher(text); // the source
    	@SuppressWarnings("unused")
		boolean b = false; 
    	while(b = m.find()) { 
    		mp.put(m.start(),m.group());
    	}  
    	return mp;
	}

	public List<String> getParts(String regex, String text) {
		List<String> parts = new ArrayList<String>();
		
		try {
			p = Pattern.compile(regex); // the expression 
			m = p.matcher(text); // the source
			@SuppressWarnings("unused")
			boolean b = false; 
    	
			int i=0;
			while(b = m.find()) { 
				parts.add(text.substring(i, m.start()));
    			parts.add(m.group());    		
    			i=m.start() + m.group().length();
			
    			//System.out.println(m.group());
			}  
			parts.add(text.substring(i,text.length()));
		} catch (Exception e) {
			//System.out.println("exception in regex");
			parts=null;
		}
    	
    	return parts;
	}
	
}
